package com.fingard.dsp.bank.directbank.cgbunion01.util;

import cfca.sadk.algorithm.common.PKIException;
import cfca.sadk.algorithm.sm2.SM2PrivateKey;
import cfca.sadk.algorithm.sm2.SM2PublicKey;
import cfca.sadk.cgb.toolkit.BASE64Toolkit;
import cfca.sadk.cgb.toolkit.SM2Toolkit;

import java.io.IOException;


public class SM2SignUtils {
	//private static final int BUFFSIZE = 0x10000;
	
	/**
	 * 签名
	 * @param srcStr
	 * @param privateKeyPath
	 * @param encoding
	 * @return
	 * @throws IOException 
	 * @throws PKIException 
	 * @throws Exception
	 */
	
	public static String signString(String srcStr,byte[] privateKeyBytes,String encoding) throws Exception{
		String signValue = null;
		try{
			SM2Toolkit sm2Tool = new SM2Toolkit();
			// String path = SM2SignUtils.class.getClassLoader().getResource("").getPath();
			// byte[] privateByte = SM2SignUtils.read(path + pvkName);
			SM2PrivateKey sm2PrivateKey = (SM2PrivateKey)sm2Tool.SM2BuildPrivateKey(BASE64Toolkit.encode(privateKeyBytes));
			signValue=BASE64Toolkit.encode(sm2Tool.SM2Sign(sm2PrivateKey, srcStr.getBytes(encoding)));
		}catch(Exception e){
			throw e;
		}
		return signValue;
	}
	
	/**
	 * 验签
	 * @param pukName
	 * @param signValue
	 * @param srcString
	 * @param encoding
	 * @return
	 * @throws Exception
	 */
	public static boolean verifySignString(byte[] publicKeyBytes,String signValue,String srcString,String encoding) throws Exception{
		boolean result = false;
		try {
			//读取公钥
			SM2Toolkit sm2Tool = new SM2Toolkit();
			// String path=SM2SignUtils.class.getClassLoader().getResource("").getPath();
			// byte[] publiceByte = SM2SignUtils.read(path + pukName);
			SM2PublicKey sm2MerPublicKey=(SM2PublicKey)sm2Tool.SM2BuildPublicKey(BASE64Toolkit.encode(publicKeyBytes));
			//验证签名
			result=sm2Tool.SM2Verify(sm2MerPublicKey,srcString.getBytes(encoding), BASE64Toolkit.decode(signValue));
		} catch (Exception e) {
			throw e;
		}
		return result;
	}
	
	/**
	 * 使用公钥加密
	 * @param pukName
	 * @param needEncryptData
	 * @param encoding
	 * @return
	 * @throws Exception
	 */
	public static String encryptString(byte[] publicKeyBytes,String needEncryptData,String encoding) throws Exception{			
		String encryptResult = "";
		try {
			SM2Toolkit sm2Tool = new SM2Toolkit();
//			String path=SM2SignUtils.class.getClassLoader().getResource("").getPath();
//			byte[] publiceByte = SM2SignUtils.read(path + pukName);
			SM2PublicKey sm2MerPublicKey=(SM2PublicKey)sm2Tool.SM2BuildPublicKey(BASE64Toolkit.encode(publicKeyBytes));
			encryptResult = BASE64Toolkit.encode(sm2Tool.SM2EncryptData(sm2MerPublicKey, needEncryptData.getBytes(encoding)));
		} catch (Exception e) {
			throw e;
		}
		return encryptResult;
	}

	/**
	 * 使用私钥解密
	 *
	 * @param pvkName
	 * @param encString
	 * @param encoding
	 * @return
	 * @throws Exception
	 */
	public static String decryptString(byte[] privateKeyBytes, String encString, String encoding) throws Exception {
		String decryptResult = null;
		try {
			SM2Toolkit sm2Tool = new SM2Toolkit();
//			String path = SM2SignUtils.class.getClassLoader().getResource("").getPath();
//			byte[] privateByte = SM2SignUtils.read(path + pvkName);
			SM2PrivateKey sm2PrivateKey = (SM2PrivateKey) sm2Tool.SM2BuildPrivateKey(BASE64Toolkit.encode(privateKeyBytes));

			byte[] decode = BASE64Toolkit.decode(encString);
			decryptResult = new String(sm2Tool.SM2DecryptData(sm2PrivateKey, decode), encoding);
		} catch (Exception e) {
			throw e;
		}
		return decryptResult;
	}
}
